<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>UVA1619 感觉不错 Feel Good（良好的感觉） 题解 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="毒瘤">
<meta property="og:type" content="article">
<meta property="og:title" content="UVA1619 感觉不错 Feel Good（良好的感觉） 题解">
<meta property="og:url" content="http://example.com/2021/07/30/UVA1619%20%E6%84%9F%E8%A7%89%E4%B8%8D%E9%94%99%20Feel%20Good%EF%BC%88%E8%89%AF%E5%A5%BD%E7%9A%84%E6%84%9F%E8%A7%89%EF%BC%89%20%E9%A2%98%E8%A7%A3/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="毒瘤">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-07-30T06:05:16.913Z">
<meta property="article:modified_time" content="2023-07-19T11:51:30.004Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-UVA1619 感觉不错 Feel Good（良好的感觉） 题解" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2021/07/30/UVA1619%20%E6%84%9F%E8%A7%89%E4%B8%8D%E9%94%99%20Feel%20Good%EF%BC%88%E8%89%AF%E5%A5%BD%E7%9A%84%E6%84%9F%E8%A7%89%EF%BC%89%20%E9%A2%98%E8%A7%A3/" class="article-date">
  <time class="dt-published" datetime="2021-07-30T06:05:16.913Z" itemprop="datePublished">2021-07-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      UVA1619 感觉不错 Feel Good（良好的感觉） 题解
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>毒瘤</p>
<h2 id="0-题面："><a href="#0-题面：" class="headerlink" title="0.题面："></a>0.题面：</h2><blockquote>
<p>给出正整数n和一个(1 &lt;= n &lt;= 100 000)长度的数列，要求找出一个子区间，使这个子区间的数字和乘上子区间中的最小值最大。输出这个最大值与区间的两个端点。</p>
</blockquote>
<h2 id="1-思路"><a href="#1-思路" class="headerlink" title="1.思路"></a>1.思路</h2><p>一开始试图使用单调栈，然而在调试一上午无果后愤然打了个分治，然后就过了233</p>
<p>根据分治三步法，算法流程分为：</p>
<p>1.分解：定义 $dfs(l,r)$ 为区间 $[l,r]$ 的最优解，$mid = (l+r)/2$  ，可以把问题分为 $dfs(l,mid)$ 和 $dfs(mid+1,r)$ 两部分，分别对应最优解完全位于左子区间和右子区间的情况。</p>
<p>2.边界：当 $l=r$ 时，$dfs(l,r)={a_l}^2$。（数列为$a$）</p>
<p>3.合并：在第一步处理了最优解完全位于左子区间和右子区间的情况，还有最优解跨越 $mid$ 的情况没处理。注意到当最小值一定时，区间越大越好，所以可以从大到小地选择最大值，再从中点开始往左右两端“放宽”当前区间。</p>
<h2 id="2-Code"><a href="#2-Code" class="headerlink" title="2.Code"></a>2.Code</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;bits/stdc++.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MAXN 1000000</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">long</span> <span class="type">long</span> ll;</span><br><span class="line"><span class="type">int</span> n;</span><br><span class="line">ll a[MAXN+<span class="number">5</span>],sum[MAXN+<span class="number">5</span>],ans;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">answ</span>&#123;</span><br><span class="line">	<span class="type">int</span> l,r;ll v;</span><br><span class="line">	<span class="built_in">answ</span>()&#123;</span><br><span class="line">		l=r=v=<span class="number">0</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">answ</span>(<span class="type">int</span> L,<span class="type">int</span> R,ll V)&#123;</span><br><span class="line">		l=L;r=R;v=V;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="type">bool</span> <span class="keyword">operator</span> &lt; (answ l,answ r)&#123;</span><br><span class="line">	<span class="keyword">return</span> l.v&lt;r.v;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">ST</span>&#123;</span><br><span class="line">	ll st[MAXN+<span class="number">1</span>][<span class="number">31</span>];</span><br><span class="line">	<span class="function"><span class="type">int</span> <span class="title">query</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;</span><br><span class="line">        <span class="type">int</span> k=<span class="built_in">log2</span>(r-l+<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">min</span>(st[l][k],st[r-(<span class="number">1</span>&lt;&lt;k)+<span class="number">1</span>][k]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">build</span><span class="params">()</span></span>&#123;</span><br><span class="line">    	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">    		st[i][<span class="number">0</span>]=a[i];</span><br><span class="line">		&#125;</span><br><span class="line">    	<span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">1</span>;j&lt;=<span class="number">30</span>;j++)&#123;</span><br><span class="line">            <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i+(<span class="number">1</span>&lt;&lt;j)<span class="number">-1</span>&lt;=n;i++)&#123;</span><br><span class="line">                st[i][j]=<span class="built_in">min</span>(st[i][j<span class="number">-1</span>],st[i+(<span class="number">1</span>&lt;&lt;(j<span class="number">-1</span>))][j<span class="number">-1</span>]);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;qwq;</span><br><span class="line"><span class="function">ll <span class="title">query</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;</span><br><span class="line">	<span class="keyword">return</span> qwq.<span class="built_in">query</span>(l,r)*(sum[r]-sum[l<span class="number">-1</span>]);</span><br><span class="line">&#125;<span class="comment">//求一个区间的“数字和乘上子区间中的最小值”</span></span><br><span class="line"><span class="function">answ <span class="title">dfs</span><span class="params">(<span class="type">int</span> l,<span class="type">int</span> r)</span></span>&#123;<span class="comment">//分治主体</span></span><br><span class="line">	<span class="type">int</span> mid=(l+r)/<span class="number">2</span>;</span><br><span class="line">	<span class="keyword">if</span>(l==r)&#123;<span class="comment">//边界</span></span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">answ</span>(l,r,<span class="built_in">query</span>(l,r));</span><br><span class="line">	&#125;</span><br><span class="line">	answ ans=<span class="built_in">max</span>(<span class="built_in">dfs</span>(l,mid),<span class="built_in">dfs</span>(mid+<span class="number">1</span>,r));</span><br><span class="line">	vector&lt;<span class="type">int</span>&gt; tmp;</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=l;i&lt;=r;i++)&#123;</span><br><span class="line">		tmp.<span class="built_in">push_back</span>(a[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">sort</span>(tmp.<span class="built_in">begin</span>(),tmp.<span class="built_in">end</span>());<span class="built_in">reverse</span>(tmp.<span class="built_in">begin</span>(),tmp.<span class="built_in">end</span>());<span class="comment">//从大到小地取区间最小值</span></span><br><span class="line">	<span class="type">int</span> L=mid,R=mid+<span class="number">1</span>;</span><br><span class="line">	ans=<span class="built_in">max</span>(ans,<span class="built_in">answ</span>(L,R,<span class="built_in">query</span>(L,R)));</span><br><span class="line">	<span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;tmp.<span class="built_in">size</span>();i++)&#123;<span class="comment">//由于最小值“逐渐放宽”，所以区间只增不减</span></span><br><span class="line">		<span class="keyword">if</span>(tmp[i]&gt;a[mid]||tmp[i]&gt;a[mid])&#123;</span><br><span class="line">			<span class="keyword">continue</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">while</span>(a[L<span class="number">-1</span>]&gt;=tmp[i]&amp;&amp;L!=l)&#123;</span><br><span class="line">			L--;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">while</span>(a[R+<span class="number">1</span>]&gt;=tmp[i]&amp;&amp;R!=r)&#123;</span><br><span class="line">			R++;</span><br><span class="line">		&#125;<span class="comment">//区间越大越好</span></span><br><span class="line">		ans=<span class="built_in">max</span>(ans,<span class="built_in">answ</span>(L,R,<span class="built_in">query</span>(L,R)));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> ans;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    cin&gt;&gt;n;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">1</span>;i&lt;=n;i++)&#123;</span><br><span class="line">    	cin&gt;&gt;a[i];</span><br><span class="line">    	sum[i]=sum[i<span class="number">-1</span>]+a[i];</span><br><span class="line">    &#125;</span><br><span class="line">    qwq.<span class="built_in">build</span>();</span><br><span class="line">    answ ans=<span class="built_in">dfs</span>(<span class="number">1</span>,n);</span><br><span class="line">    cout&lt;&lt;ans.v&lt;&lt;endl;</span><br><span class="line">    cout&lt;&lt;ans.l&lt;&lt;<span class="string">&quot; &quot;</span>&lt;&lt;ans.r&lt;&lt;endl;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/07/30/UVA1619%20%E6%84%9F%E8%A7%89%E4%B8%8D%E9%94%99%20Feel%20Good%EF%BC%88%E8%89%AF%E5%A5%BD%E7%9A%84%E6%84%9F%E8%A7%89%EF%BC%89%20%E9%A2%98%E8%A7%A3/" data-id="clk9o8ln200058wi58fyu9r3a" data-title="UVA1619 感觉不错 Feel Good（良好的感觉） 题解" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/08/11/%E7%82%B9%E5%88%86%E6%B2%BB%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          点分治 学习笔记
        
      </div>
    </a>
  
  
    <a href="/2021/07/29/%E5%B1%B1%E6%B5%B7%E7%BB%8F%20%E9%A2%98%E8%A7%A3/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">山海经 题解</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>